import random

cards = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'K', 'Q'] * 4
score_list = {'K': 10, 'Q': 10, 'J': 10,
              '10': 10, '9': 9, '8': 8, '7': 7,
              '6': 6, '5': 5, '4': 4, '3': 3, '2': 2}


class Game(object):
    def __init__(self, person_num):
        self.person_num = person_num
        self.cards = [[] for i in range(person_num)]
        self.scores = [0 for i in range(person_num)]
        self.winner = None
        self.send_num = 0

    def print_state(self):
        self.get_Count()
        self.send_num = 0
        print(self.cards)
        print("Leftover cards:{}".format(len(cards)))
        for x in range(0, int(self.person_num)):
            print("Cards of the {} : {}".format(x + 1, self.cards[x]))

    def call_card(self):
        for x in range(0, int(self.person_num)):
            if self.scores[x] >= 21:
                continue
            send = input(f"Send one card for {x + 1}?(y/n)")
            if send == 'y':
                self.cards[x].append(self.get_outCard())
                self.send_num += 1

    def get_Winner(self):
        max_score = 0
        for score in self.scores:
            if max_score < score <= 21:
                max_score = score
        if max_score != 0:
            self.winner = self.scores.index(max_score) + 1
            print(f"Winner is {self.winner}!!!")
        else:
            print(" No winner in this game!!!")

    def get_outCard(self):
        return cards.pop(random.randint(0, len(cards) - 1))

    def get_Count(self):
        for x in range(0, self.person_num):
            self.scores[x] = 0
            if 'A' in self.cards[x]:
                self.cards[x].pop(self.cards.index('A'))
                self.cards[x].append('A')
            for card in self.cards[x]:
                if card == 'A':
                    self.scores[x] += 1 if self.scores[x] + 11 > 21 else 11
                else:
                    self.scores[x] += score_list[card]


def main():
    game1 = Game(int(input(" input player's num：")))
    while game1.winner == None:
        game1.print_state()
        game1.call_card()
        if game1.send_num == 0:
            game1.get_Winner()


if __name__ == '__main__':
    main()
